﻿/* Author: Achim Schnell */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AppFx.Tokenization
{
    /// <summary>
    /// Verifies bool-literals for tokens.
    /// </summary>
    public class BooleanVerifier : Verifier
    {
        #region Fields

        /// <summary>
        /// A counter for performed checks after last reset.
        /// </summary>
        private int checkCount = 0;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the trueexpression.
        /// </summary>
        /// <value>
        /// The trueexpression.
        /// </value>
        public string TrueExpression { get; set; }

        /// <summary>
        /// Gets or sets the falseexpression.
        /// </summary>
        /// <value>
        /// The falseexpression.
        /// </value>
        public string FalseExpression { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SymbolVerifier"/> class.
        /// </summary>
        /// <param name="tokenizer">The parent tokenizer.</param>
        public BooleanVerifier(ITokenizer tokenizer) : this(tokenizer, "true", "false") { }

        /// <summary>
        /// Initializes a new instance of the <see cref="BooleanVerifier"/> class.
        /// </summary>
        /// <param name="tokeinzer">The parent tokenizer.</param>
        /// <param name="trueExpression">The trueexpression.</param>
        /// <param name="falseExpression">The falseexpression.</param>
        public BooleanVerifier(ITokenizer tokeinzer, string trueExpression, string falseExpression) : base(tokeinzer)
        {
            TrueExpression = trueExpression;
            FalseExpression = falseExpression;
        }

        #endregion

        #region Verifier

        /// <summary>
        /// Resets the verifiers buffer and counter.
        /// </summary>
        public override void Reset()
        {
            checkCount = 0;
            base.Reset();
        }

        /// <summary>
        /// Detects if the given char matches the token's pattern.
        /// </summary>
        /// <param name='c'>Char to verify for tokenization.</param>
        /// <returns>
        /// A bool indicating if the given char matches the token's pattern.
        /// </returns>
        public override bool Verify(char c)
        {
            checkCount = checkCount + 1;
            if (checkCount <= TrueExpression.Length && TrueExpression[checkCount-1].Equals(c))
                return true;
            else if (checkCount <= FalseExpression.Length && FalseExpression[checkCount-1].Equals(c))
                return true;
            else
                return false;
        }

        /// <summary>
        /// Creates the token based on the current input-pattern.
        /// </summary>
        /// <returns>
        /// An IToken containing the current input-pattern.
        /// </returns>
        protected override IToken CreateToken()
        {
            if (Buffer.Equals(TrueExpression) || Buffer.Equals(FalseExpression))
                return new Token(Buffer, eTokenType.BooleanLiteral);
            throw new Exception();
        }

        #endregion
    }
}
